﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Collections;

namespace Phong_CTSV
{
    public class Export
    {
        Form form = null;
        String formName = null;
        long uni = 0;
        List<Columns_Header> columnsHeader = null;
        List<Rows_Header> rowsHeader = null;
        Data_Table[,] dataTable = null;
        Data_Table[] Sum = null;
        int numColumn = 0;
        int numRow = 0;
        QuerrySQL query = new QuerrySQL();
        bool hasRowsHeader = false;
        Table tableExport = new Table();
        TableCell cellExport = new TableCell();
        TableRow rowExport = new TableRow();
        string uniName;
        int yearReport;
        public Export(Data_Table[,] data,Data_Table[] Sum, string uni_Name, string form_Name, int year)
        {
            uni = query.Select_UniversityByUser(uni_Name).ID;
            uniName = query.Select_UniversityByUser(uni_Name).Name;
            formName = form_Name;
            form = query.Select_Form(formName);
            rowsHeader = query.Select_RowsHeaderByForm(formName);
            columnsHeader = query.Select_ColumnsHeaderByForm(formName);
            getNumRowAndColumn();
            getDataTable(data);
            getSum(Sum);
            hasRowsHeader = form.Has_rows;
            yearReport = year;
            
        }
        private void getDataTable(Data_Table[,] data)
        {
            try
            {
                dataTable = new Data_Table[numRow, numColumn];

                for (int i = 0; i < numColumn; i++)
                {
                    for (int j = 0; j < numRow; j++)
                    {
                        dataTable[j, i] = data[j, i];
                    }
                }
            }
            catch (Exception ex) { }
        }
        private void getNumRowAndColumn()
        {
            foreach (Rows_Header rh in rowsHeader)
            {
                if (!rh.Has_child)
                {
                    numRow++;
                }

            }

            foreach (Columns_Header ch in columnsHeader)
            {
                if (!ch.Has_child)
                {
                    numColumn++;
                }
            }

            numColumn--;
        }
        private void getSum(Data_Table[] sum)
        {
            Sum = new Data_Table[numColumn];
            for (int i = 0; i < numColumn; i++)
                Sum[i] = sum[i];
        }
        public List<Columns_Header> ColumnsHeaderList
        {
            get
            {
                return columnsHeader;
            }
        }
        private int NumColumn
        {
            get
            {
                return numColumn;
            }
        }
        private List<Rows_Header> RowsHeaderList
        {
            get
            {
                return rowsHeader;
            }
        }

        private void createHeaderExport()
        {
            TableRow rowHeader1 = new TableRow();
            TableCell cellHeader1 = new TableCell();
            cellHeader1.RowSpan = 1;
            cellHeader1.ColumnSpan = numColumn;
            cellHeader1.Text = "Cộng hòa xã hội chủ nghĩa Việt Nam";
            cellHeader1.Font.Bold = true;
            cellHeader1.HorizontalAlign = HorizontalAlign.Center;
            rowHeader1.Cells.Add(cellHeader1);
            tableExport.Rows.Add(rowHeader1);
            
            TableRow rowHeader2 = new TableRow();
            TableCell cellHeader2 = new TableCell();
            cellHeader2.RowSpan = 1;
            cellHeader2.ColumnSpan = numColumn;
            cellHeader2.Text = "Độc lập- Tự do- Hạnh phúc";
            cellHeader2.Font.Bold = true;
            cellHeader2.HorizontalAlign = HorizontalAlign.Center;
            rowHeader2.Cells.Add(cellHeader2);
            tableExport.Rows.Add(rowHeader2);
            

            TableRow rowHeader3 = new TableRow();
            TableCell cellHeader3 = new TableCell();
            cellHeader3.RowSpan = 1;
            cellHeader3.ColumnSpan = 10;
            cellHeader3.Text = "Trường: " +uniName;
            cellHeader3.Font.Bold = true;
            cellHeader3.HorizontalAlign = HorizontalAlign.Left;
            rowHeader3.Cells.Add(cellHeader3);
            tableExport.Rows.Add(rowHeader3);

            TableRow rowHeader4 = new TableRow();
            TableCell cellHeader4 = new TableCell();
            cellHeader4.RowSpan = 1;
            cellHeader4.ColumnSpan = 10;
            cellHeader4.Text = "Năm: " + yearReport.ToString();
            cellHeader4.Font.Bold = true;
            cellHeader4.HorizontalAlign = HorizontalAlign.Left;
            rowHeader4.Cells.Add(cellHeader4);
            tableExport.Rows.Add(rowHeader4);
            
            TableRow rowHeader5 = new TableRow();
            TableCell cellHeader5 = new TableCell();
            cellHeader5.RowSpan = 1;
            cellHeader5.ColumnSpan = numColumn;
            cellHeader5.Text = form.Decription;
            cellHeader5.Font.Bold = true;
            cellHeader5.Font.Size = 15;
            cellHeader5.BorderStyle = BorderStyle.Solid;
            cellHeader5.HorizontalAlign = HorizontalAlign.Center;
            rowHeader5.Cells.Add(cellHeader5);
            tableExport.Rows.Add(rowHeader5);
        }
        private void createColumnHeader()
        {
            TableRow rowColumnHeader = new TableRow();
            TableRow rowColumnHeader_Child = new TableRow();
            
            bool row2Header = false; 
            List<Columns_Header> column_Header_Child = new List<Columns_Header>();
            foreach (Phong_CTSV.Columns_Header ch in ColumnsHeaderList)
            {

                column_Header_Child.Clear();
                if (ch.Parent_id == 0)
                {
                    int child = 0;
                    foreach (Phong_CTSV.Columns_Header childHeader in ColumnsHeaderList)
                    {
                        if (childHeader.Parent_id == ch.ID)
                        {
                            child++;
                            column_Header_Child.Add(childHeader);
                            row2Header = true;
                        }
                    }
                    if(child == 0)
                    {
                        TableCell cellColumnHeader = new TableCell();
                        cellColumnHeader.RowSpan = 1;
                        cellColumnHeader.ColumnSpan = 1;
                        cellColumnHeader.Text = ch.Name;
                        cellColumnHeader.Font.Bold = true;
                        cellColumnHeader.HorizontalAlign = HorizontalAlign.Center;
                        rowColumnHeader.Cells.Add(cellColumnHeader);

                        TableCell cellColumnHeader_Child = new TableCell();
                        cellColumnHeader_Child.RowSpan = 1;
                        cellColumnHeader_Child.ColumnSpan = 1;
                        cellColumnHeader_Child.Text = "";
                        cellColumnHeader_Child.HorizontalAlign = HorizontalAlign.Center;
                        rowColumnHeader_Child.Cells.Add(cellColumnHeader_Child);
                    }
                    else
                    {
                        TableCell cellColumnHeader = new TableCell();
                        cellColumnHeader.RowSpan = 1;
                        cellColumnHeader.ColumnSpan = child;
                        cellColumnHeader.Text = ch.Name;
                        cellColumnHeader.Font.Bold = true;
                        cellColumnHeader.HorizontalAlign = HorizontalAlign.Center;
                        rowColumnHeader.Cells.Add(cellColumnHeader);
                        foreach (Columns_Header child_Hader in column_Header_Child)
                        {
                            TableCell cellColumnHeader_Child = new TableCell();
                            cellColumnHeader_Child.RowSpan = 1;
                            cellColumnHeader_Child.ColumnSpan = 1;
                            cellColumnHeader.Font.Bold = true;
                            cellColumnHeader_Child.Text = child_Hader.Name;
                            cellColumnHeader_Child.HorizontalAlign = HorizontalAlign.Center;
                            rowColumnHeader_Child.Cells.Add(cellColumnHeader_Child);
                        }
                    }
                    
                }
            }
            tableExport.Rows.Add(rowColumnHeader);
            if(row2Header)
                tableExport.Rows.Add(rowColumnHeader_Child);
        }
        private void createRowAndData()
        {
            int rowIndex = 0;            
            foreach (Phong_CTSV.Rows_Header rh in RowsHeaderList)
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                cell.RowSpan = 1;
                cell.ColumnSpan = 1;
                cell.Text = rh.Name;
                cell.Font.Bold = true;
                cell.HorizontalAlign = HorizontalAlign.Center;
                row.Cells.Add(cell);

                for (int i = 0; i < NumColumn; i++)
                {
                    TableCell cell_Data = new TableCell();
                    cell_Data.RowSpan = 1;
                    cell_Data.ColumnSpan = 1;
                    cell_Data.Text = dataTable[rowIndex, i].Value.ToString();
                    cell_Data.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell_Data);

                }
                rowIndex++;
                tableExport.Rows.Add(row);
            }

        }
        private void createRowAndData(Data_Table[,] data)
        {
            int rowIndex = 0;
            foreach (Phong_CTSV.Rows_Header rh in RowsHeaderList)
            {
                TableRow row = new TableRow();
                TableCell cell = new TableCell();
                cell.RowSpan = 1;
                cell.ColumnSpan = 1;
                cell.Text = rh.Name;
                cell.Font.Bold = true;
                cell.HorizontalAlign = HorizontalAlign.Center;
                row.Cells.Add(cell);

                for (int i = 0; i < NumColumn; i++)
                {
                    TableCell cell_Data = new TableCell();
                    cell_Data.RowSpan = 1;
                    cell_Data.ColumnSpan = 1;
                    cell_Data.Text = data[rowIndex, i].Value.ToString();
                    cell_Data.HorizontalAlign = HorizontalAlign.Center;
                    row.Cells.Add(cell_Data);

                }
                rowIndex++;
                tableExport.Rows.Add(row);
            }

        }
        private void addTotalRow()
        {
            
            TableRow row = new TableRow();
            TableRow row1 = new TableRow();// tach hang tinh tong ra rieng
            if (form.Has_rows)
            {
                TableCell cell_Data = new TableCell();
                cell_Data.RowSpan = 1;
                cell_Data.ColumnSpan = 1;
                cell_Data.Text = "Tổng:";
                cell_Data.Font.Bold = true;
                cell_Data.HorizontalAlign = HorizontalAlign.Center;
                row.Cells.Add(cell_Data);
            }
            else
            {
                TableCell cell_Data = new TableCell();
                cell_Data.RowSpan = 1;
                cell_Data.ColumnSpan = 1;
                cell_Data.Text = "Tổng:";
                cell_Data.Font.Bold = true;
                cell_Data.HorizontalAlign = HorizontalAlign.Center;
                row1.Cells.Add(cell_Data);
            }
            for (int i = 0; i < numColumn; i++)
            {
                TableCell cell_Data = new TableCell();
                cell_Data.RowSpan = 1;
                cell_Data.ColumnSpan = 1;
                cell_Data.Text = Sum[i].Value;
                cell_Data.HorizontalAlign = HorizontalAlign.Center;
                row.Cells.Add(cell_Data);

            }

            if (form.Has_rows)
                tableExport.Rows.Add(row);
            else
            {
                tableExport.Rows.Add(row1);
                tableExport.Rows.Add(row);
            }
  
        }
        
        public string CreateExcelFile()
        {
            createHeaderExport();
            createColumnHeader();
            createRowAndData();
            addTotalRow();
            TableRow row = new TableRow();
            TableCell cell_Data = new TableCell();
            cell_Data.RowSpan = 1;
            cell_Data.ColumnSpan = numColumn;
            cell_Data.Text = "Tp HCM, Ngày\t Tháng\t Năm\t";
            cell_Data.HorizontalAlign = HorizontalAlign.Right;
            row.Cells.Add(cell_Data);
            tableExport.Rows.Add(row);
            TableItemStyle tableStyle = new TableItemStyle();
            tableStyle.HorizontalAlign = HorizontalAlign.Center;
            tableStyle.VerticalAlign = VerticalAlign.Middle;
            tableStyle.Width = Unit.Pixel(100);
            tableExport.ApplyStyle(tableStyle);
            
            System.IO.StringWriter writer = new System.IO.StringWriter();
            System.Web.UI.Html32TextWriter html = new System.Web.UI.Html32TextWriter(writer);
            tableExport.RenderControl(html);
            String output = writer.ToString();
            return output;

        }

        public string CreateExcelFile(Data_Table[,] data)
        {
            createHeaderExport();
            createColumnHeader();
            createRowAndData(data);
            addTotalRow();
            TableRow row = new TableRow();
            TableCell cell_Data = new TableCell();
            cell_Data.RowSpan = 1;
            cell_Data.ColumnSpan = numColumn;
            cell_Data.Text = "Tp HCM, Ngày\t Tháng\t Năm\t";
            cell_Data.HorizontalAlign = HorizontalAlign.Right;
            row.Cells.Add(cell_Data);
            tableExport.Rows.Add(row);
            TableItemStyle tableStyle = new TableItemStyle();
            tableStyle.HorizontalAlign = HorizontalAlign.Center;
            tableStyle.VerticalAlign = VerticalAlign.Middle;
            tableStyle.Width = Unit.Pixel(100);
            tableExport.ApplyStyle(tableStyle);

            System.IO.StringWriter writer = new System.IO.StringWriter();
            System.Web.UI.Html32TextWriter html = new System.Web.UI.Html32TextWriter(writer);
            tableExport.RenderControl(html);
            String output = writer.ToString();
            return output;
        }
        

    }
}